package com.geeklan.base.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

public class BitmapUtils {
    public static Bitmap imageVague(Context context , int drawable){
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawable);
        Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
        RenderScript rs = RenderScript.create(context);
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        //设置模糊半径取值0-25之间，不同半径得到的模糊效果不同
        blurScript.setRadius(25);
        blurScript.setInput(tmpIn);
        blurScript.forEach(tmpOut);
        //得到最终的模糊bitmap
        tmpOut.copyTo(outputBitmap);
        bitmap.recycle();
        inputBitmap.recycle();
        return outputBitmap;
    }
}
